//
//  SIStatusCell.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/5.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

///微博cell的协议,告诉选中的cell和需要的urlString
@objc protocol SIStatusCellDelegate: NSObjectProtocol {
    
    @objc optional func statusCellDidSelectedUrlString(cell: SIStatusCell, urlString: String)
    
}

class SIStatusCell: UITableViewCell {
    
    /// 代理属性
    weak var delegate: SIStatusCellDelegate?
    
    ///微博视图模型
    var viewModel: SIStatusViewModel? {
        
        didSet {
            
            ///详情
            contentLabel.attributedText = viewModel?.statusAttrText
            ///昵称
            nameLabel.text = viewModel?.status.user?.screen_name
            ///设置会员图标 - 直接获取属性,不需要计算
            memberIconView.image = viewModel?.memberIcon
            
            ///认证图标
            vipIcon.image = viewModel?.vipIcon
            
            ///用户头像
            iconView.cz_setImage(urlString: viewModel?.status.user?.profile_image_url, placeholderImage: #imageLiteral(resourceName: "avatar_default_big"), isAvatar: true)
            ///底部工具栏
            toolBarView.viewModel = viewModel
            
            ///视图模型
            picturesView.viewModel = viewModel

            ///设置被转发微博的文字
            retransmissionLabel?.attributedText = viewModel?.retweetedAttrText   //viewModel?.retransmissionText
            
            ///来源url字符串
            sourceLabel.text = viewModel?.status.source  //viewModel?.status.source
            
            /// 设置时间
            timeLabel.text = viewModel?.status.createDate?.cz_dateDescription
            
        }
    }

    @IBOutlet weak var vipIcon: UIImageView!
    /// 头像视图
    @IBOutlet weak var iconView: UIImageView!
    ///昵称
    @IBOutlet weak var nameLabel: UILabel!
    ///发表时间
    @IBOutlet weak var timeLabel: UILabel!
    ///会员图标
    @IBOutlet weak var memberIconView: UIImageView!
    ///来源
    @IBOutlet weak var sourceLabel: UILabel!
    ///详情
    @IBOutlet weak var contentLabel: FFLabel!
    ///底部视图
    @IBOutlet weak var toolBarView: SIStatusToolBar!
    ///图片视图
    @IBOutlet weak var picturesView: SIStatusPictureView!
    
    @IBOutlet weak var retransmissionLabel: FFLabel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        ///离屏渲染 - 异步绘制
        self.layer.drawsAsynchronously = true
        ///栅格化
        self.layer.shouldRasterize = true
        ///使用栅格化必须制定分辨率
        self.layer.rasterizationScale = UIScreen.main().scale
        
        contentLabel.delegate = self
        
        retransmissionLabel?.delegate = self
    }

    
    
}

extension SIStatusCell: FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        if !text.hasPrefix("http://") {
            
            return
        }
        
        delegate?.statusCellDidSelectedUrlString?(cell: self, urlString: text)
    }
    
}
